package com.system.controller;

import com.system.pojo.Product;
import com.system.service.ProductService;
import com.system.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/insert")
    public ResultVo<Product> insert(Product product) {
		ResultVo<Product> resultVo = new ResultVo<>();
		try {
			resultVo = productService.insert(product);
			if (resultVo.isInsert()){
				resultVo.setMessage("添加产品成功");
			}
		} catch (Exception e) {
            e.printStackTrace();
			resultVo.setMessage("添加产品失败");
        }finally {
        	return resultVo;
		}

    }

    @RequestMapping("/update")
    public ResultVo<Product> update(Product product) {
    	ResultVo<Product> resultVo = new ResultVo<>();
    	try {
			resultVo = productService.update(product);
		}catch (Exception e){
    		e.printStackTrace();
    		resultVo.setMessage("更新产品失败");
		}finally {
    		return resultVo;
		}
    }

    @RequestMapping("/selectAll")
    public ResultVo<Product> selectAll() {
		ResultVo<Product> resultVo = new ResultVo<>();
		try {
			resultVo = productService.selectAll();
		}catch (Exception e){
			e.printStackTrace();
			resultVo.setMessage("查询产品失败");
		}finally {
			return resultVo;
		}
    }

	@RequestMapping("/select/{productId}")
	public ResultVo<Product> selectAll(@PathVariable Integer productId) {
    	System.out.println("进来了");
		ResultVo<Product> resultVo = new ResultVo<>();
		try {
			resultVo = productService.select(productId);
		}catch (Exception e){
			e.printStackTrace();
			resultVo.setMessage("查询产品失败");
		}finally {
			return resultVo;
		}
	}

	@RequestMapping("/delete/{productId}")
	public ResultVo<Product> delete(@PathVariable Integer productId){
		ResultVo<Product> resultVo = new ResultVo<Product>();
		try {
			resultVo = productService.delete(productId);
		}catch (Exception e){
			e.printStackTrace();
			resultVo.setMessage("删除失败");
		}finally {
			return resultVo;
		}

	}


	@RequestMapping("/deleteProducts")
	public ResultVo<Product> deleteProducts(@RequestBody List<Integer> productIds){
		ResultVo<Product> resultVo = new ResultVo<Product>();
		try {
//			System.out.println(productIds);
			resultVo = productService.deleteProducts(productIds);
		}catch (Exception e){
			e.printStackTrace();
			resultVo.setMessage("删除失败");
		}finally {
			return resultVo;
		}

	}
}
